package com.grj.parking.domain;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import java.io.Serializable;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Date;
import lombok.Data;
import org.elasticsearch.xcontent.XContentBuilder;
import org.elasticsearch.xcontent.XContentFactory;

/**
 * 停车场信息表（含经纬度）
 * @TableName parking_lot
 */
@TableName(value ="parking_lot")
@Data
public class ParkingLot implements Serializable {
    /**
     * 停车场ID
     */
    @TableId(value = "id", type = IdType.AUTO)
    private Long id;

    /**
     * 租户ID
     */
    @TableField(value = "tenant_id")
    private Long tenantId;

    /**
     * 停车场名称
     */
    @TableField(value = "lot_name")
    private String lotName;

    /**
     * 停车场编码
     */
    @TableField(value = "lot_code")
    private String lotCode;

    /**
     * 地址
     */
    @TableField(value = "address")
    private String address;

    /**
     * 纬度（范围-90~90，保留6位小数）
     */
    @TableField(value = "latitude")
    private BigDecimal latitude;

    /**
     * 经度（范围-180~180，保留6位小数）
     */
    @TableField(value = "longitude")
    private BigDecimal longitude;

    /**
     * 总车位数
     */
    @TableField(value = "total_spaces")
    private Integer totalSpaces;

    /**
     * 可用车位数
     */
    @TableField(value = "available_spaces")
    private Integer availableSpaces;

    /**
     * 营业时间
     */
    @TableField(value = "opening_hours")
    private String openingHours;

    /**
     * 联系电话
     */
    @TableField(value = "contact_phone")
    private String contactPhone;

    /**
     * 状态(0-关闭,1-正常)
     */
    @TableField(value = "status")
    private Integer status;

    /**
     * 创建时间
     */
    @TableField(value = "create_time")
    private LocalDateTime createTime;

    /**
     * 更新时间
     */
    @TableField(value = "update_time")
    private LocalDateTime updateTime;

    @TableField(exist = false)
    private static final long serialVersionUID = 1L;



    // 非数据库字段，用于存储距离
    @TableField(exist = false)
    private Double distance;

    // 转换为ES文档格式
    public XContentBuilder toEsDocument() throws Exception {
        return XContentFactory.jsonBuilder()
                .startObject()
                .field("id", id)
                .field("tenantId", tenantId)
                .field("lotName", lotName)
                .field("lotCode", lotCode)
                .field("address", address)
                .field("location", String.format("%s,%s", latitude, longitude)) // 格式: "lat,lon"
                .field("totalSpaces", totalSpaces)
                .field("availableSpaces", availableSpaces)
                .field("openingHours", openingHours)
                .field("contactPhone", contactPhone)
                .field("status", status)
                .field("createTime", createTime)
                .field("updateTime", updateTime)
                .endObject();
    }
}